package com.mifengs.mall.ui.msg;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mifengs.mall.R;
import com.mifengs.mall.adapter.MsgCenterAdapter;
import com.mifengs.commonbase.base.BaseFragment;
import com.mifengs.mall.di.component.DaggerMsgCenterComponent;
import com.mifengs.mall.di.module.MsgCenterModule;
import com.mifengs.mall.entity.MsgBean;
import com.mifengs.mall.util.ToIntentUtil;
import com.mifengs.mall.util.UIUtil;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MsgCenteFragment extends BaseFragment<MsgCenterPresent> implements MsgCenterContract.View, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipe_container)
    SwipeRefreshLayout mSwipeContainer;
    private MsgCenterAdapter mMsgCenterAdapter;

    private List<MsgBean.RecordListBean> msgRecordList = new ArrayList<>();
    private List<MsgBean> MsgList = new ArrayList<>();
    private View view;
    private static final int TOTAL_COUNTER = 3;
    private static final int PAGE_SIZE = 6;
    private int mCurrentCounter = 0;
    private final int INT_MY_MSG = 0;
    private final int INT_SYSTEM_MSG = 1;
    private final int INT_PROMOTION_MSG = 2;
    private int position;//title的位置
    private int page = 1;
    private boolean isLoadMore = true;//当前是否有分页
    private int TotalCounter = 0;

    public static MsgCenteFragment newInstance() {
        MsgCenteFragment fragment = new MsgCenteFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_msg_center, container, false);
        }
        ButterKnife.bind(this, view);
        setSwipeContainer();
        position = FragmentPagerItem.getPosition(getArguments());
        init();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public void initInjector() {
        DaggerMsgCenterComponent.builder()
                .msgCenterModule(new MsgCenterModule(this))
                .build()
                .inject(this);
    }

    private void init() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mMsgCenterAdapter = new MsgCenterAdapter(MsgList, getContext());
        mMsgCenterAdapter.openLoadAnimation();
        mCurrentCounter = mMsgCenterAdapter.getItemCount();
        mMsgCenterAdapter.setOnLoadMoreListener(this);
        mRecyclerView.setAdapter(mMsgCenterAdapter);
        if (position == 0) {
            mPresenter.getMyMsgData();
        } else {
            mPresenter.getMsgData(position);
        }
//        mMsgCenterAdapter.setEnableLoadMore(false);
//        mMsgCenterAdapter.loadMoreEnd();
        mMsgCenterAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int index) {
                if (position == 2) {
                    MsgBean.RecordListBean recordListBean = msgRecordList.get(index);
                    ToIntentUtil.toCommonIntent(recordListBean.getRedirectUrl(), getContext());
                }
            }
        });
    }


    private void setSwipeContainer() {
        mSwipeContainer.setDistanceToTriggerSync(UIUtil.SwipeDistance); //设置手指移动多少触发刷新
        mSwipeContainer.setOnRefreshListener(this);
        mSwipeContainer.setOnChildScrollUpCallback(new SwipeRefreshLayout.OnChildScrollUpCallback() {

            @Override
            public boolean canChildScrollUp(SwipeRefreshLayout parent, @Nullable View child) {
                if (mRecyclerView == null) {
                    return false;
                }
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
                return linearLayoutManager.findFirstCompletelyVisibleItemPosition() != 0;
            }
        });
    }

    @Override
    public void onLoadMoreRequested() {
//        if (isLoadMore) {
//            mRecyclerView.post(new Runnable() {
//                @Override
//                public void run() {
//                    int page = mMsgCenterAdapter.getData().size();
//                    if(TotalCounter != mMsgCenterAdapter.getData().size()){
//                        page++;
//                        mPresenter.getMsgData(position);
//                        mMsgCenterAdapter.setNewData(mDataList);
//                        mMsgCenterAdapter.loadMoreComplete();
//                    }
//                }
//            });
//        } else {
//            mRecyclerView.post(new Runnable() {
//                @Override
//                public void run() {
//                    mMsgCenterAdapter.loadMoreEnd();
//                    isLoadMore = false;
//                }
//            });
//        }

        if (mMsgCenterAdapter.getData().size() >= TotalCounter) {
            mRecyclerView.post(new Runnable() {
                @Override
                public void run() {
                    mMsgCenterAdapter.loadMoreEnd();
                }
            });
        }


    }

    @Override
    public void onRefresh() {
        mRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                page = 1;
                MsgList.clear();
                if (position == 0) {
                    mPresenter.getMyMsgData();
                } else {
                    mPresenter.getMsgData(position);
                }
                mMsgCenterAdapter.setEnableLoadMore(true);
                mMsgCenterAdapter.setNewData(MsgList);
                mSwipeContainer.setRefreshing(false);


            }
        });
    }

    @OnClick(R.id.swipe_container)
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
        }
    }

    @Override
    public int setPage() {
        return page;
    }

    @Override
    public void getRecordCount(boolean iMore) {
        isLoadMore = iMore;
    }

    @Override
    public void getTotalCounter(int count) {
        TotalCounter = count;
    }

    @Override
    public void setMsgContent(MsgBean msgBean, int index) {
        msgRecordList = msgBean.getRecordList();
        for (int i = 0; i < msgRecordList.size(); i++) {
            MsgBean.RecordListBean recordListBean = msgRecordList.get(i);
            if (index == INT_PROMOTION_MSG) {
                MsgList.add(new MsgBean(MsgBean.MSG_IMG, recordListBean));
            } else if (index == INT_SYSTEM_MSG) {
                MsgList.add(new MsgBean(MsgBean.MSG_TEXT, recordListBean));
            } else {
                MsgList.add(new MsgBean(MsgBean.MSG_TEXT, recordListBean));
            }

        }
        mRecyclerView.setAdapter(mMsgCenterAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
